package com.thenie.chat.config;

import com.thenie.chat.interceptor.HasLoginInterceptor;
import com.thenie.chat.interceptor.NotLoginInterceptor;
import com.thenie.chat.validator.CurrentUserArgumentResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

/**
 * @Description
 * @Author thenie
 * @Data 2022/5/19 14:13
 */
@Configuration
public class MyWebMvcConfig extends WebMvcConfigurationSupport {

    @Autowired
    private NotLoginInterceptor notloginInterceptor;
    @Autowired
    private HasLoginInterceptor hasLoginInterceptor;

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        //拦截主页面未登录的请求
        registry.addInterceptor(notloginInterceptor)
                .addPathPatterns("/**")//添加该拦截器的拦截路径
                .excludePathPatterns("/login","/register","/login/**","/register/**","/forgot","/forgot/**",//绿色请求
                        "/css/**","/fonts/**","/images/**","/js/**","/layer/**","/layui/**","/plugins.slick/**","/error/**","/world/**","/error-world-window/**");//绿色静态资源
        //拦截登陆、注册页面已经登陆过的请求
        registry.addInterceptor(hasLoginInterceptor).addPathPatterns("/login","/login/**","/register","/register/**");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
    }

    /**
     * Controller 配置参数解析器
     * @param argumentResolvers
     */
    @Override
    public void addArgumentResolvers(List argumentResolvers) {
        argumentResolvers.add(new CurrentUserArgumentResolver());
    }

}
